package com.buyouchina.bean.product;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.buyouchina.bean.area.Province;
import com.buyouchina.bean.privilege.User;

@Entity
@Table(name = "orderForm")
public class OrderForm {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	// 订单编号
	private String code;
	// 销售金额
	private Double amount;
	// 折后金额
	private String discountedAmount;
	// 收款金额
	private String receivablesAmount;
	// 总数量
	private Double totalQuantity;
	// 备注
	private String remark;
	// 操作时间
	private Date date;
	// 状态
	private String status;
	//	地区
	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "province_id")
	private Province province;

	// 类型(销售单&发货单)
	@Enumerated(EnumType.STRING)
	@Column(length = 15)
	private Type type;

	// 操作人员
	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "operator_id")
	private User operator;

	// 收货直销店
	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "outlets_id")
	private Outlets outlets;

	// 发货店铺
	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "from_storeid")
	private Outlets from;

	@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinTable(name = "productRecord_order",
	joinColumns = { @JoinColumn(name = "order_id", referencedColumnName = "id") }, 
	inverseJoinColumns = { @JoinColumn(name = "record_id", referencedColumnName = "id") })
	private Set<Record> records = new HashSet<Record>();

	public enum Type {
		SELLSORDER {
			public String getName() {
				return "销售单";
			}
		},
		PURCHASESORDER {
			public String getName() {
				return "进货单";
			}
		};
		public abstract String getName();
	}

	public OrderForm() {
		this.date = new Date();
		this.code = this.createCode();
	}

	
	public String createCode() {
		return "BUYOU" + System.currentTimeMillis();
	}

	// getter and setter

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public String getDiscountedAmount() {
		return discountedAmount;
	}

	public void setDiscountedAmount(String discountedAmount) {
		this.discountedAmount = discountedAmount;
	}

	public String getReceivablesAmount() {
		return receivablesAmount;
	}

	public void setReceivablesAmount(String receivablesAmount) {
		this.receivablesAmount = receivablesAmount;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Outlets getOutlets() {
		return outlets;
	}

	public void setOutlets(Outlets outlets) {
		this.outlets = outlets;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public User getOperator() {
		return operator;
	}

	public void setOperator(User operator) {
		this.operator = operator;
	}

	public Double getTotalQuantity() {
		return totalQuantity;
	}

	public void setTotalQuantity(Double totalQuantity) {
		this.totalQuantity = totalQuantity;
	}

	public Outlets getFrom() {
		return from;
	}

	public void setFrom(Outlets from) {
		this.from = from;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OrderForm other = (OrderForm) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}


	public void setRecords(Set<Record> records) {
		this.records = records;
	}


	public Set<Record> getRecords() {
		return records;
	}


	public Province getProvince() {
		return province;
	}


	public void setProvince(Province province) {
		this.province = province;
	}
}
